package stuman.activity;


import stuman.entity.Student;
import stuman.entity.Subject;
import stuman.util.Container;
import stuman.util.SaveLoad;

import com.StuMan.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class OpenPage extends Activity {
    /** Called when the activity is first created. */
	
	private Student student;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		SaveLoad.load(getApplicationContext());
		
		if (Container.students.isEmpty() == false){
			student = Container.students.get(0);	
			loadStudentInformation();
		}
		else {
			Intent intent  = new Intent(OpenPage.this, Login.class);
			startActivity(intent);
		}
		
	}
	
	@Override
	protected void onResume() {
		if (student != null){
			loadStudentInformation();	
		}
		
		super.onResume();
	}
	
	private void loadStudentInformation(){
		TextView welcome = (TextView) findViewById(R.id.lblWelcome);
		welcome.setText("Welcome " + student.getFirstName().toString());
		
		TableLayout tl = (TableLayout) findViewById(R.id.currentClassTable);
		tl.removeAllViews();
		for (int i=0;i<student.getSubjects().size();i++){
			TableRow tr = new TableRow(this);
			final TextView tv = new TextView(this);
			tv.setTextSize(20);
			tv.setText(student.getSubjects().get(i).getName());
			
			
			OnClickListener ocl = new View.OnClickListener(){

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(OpenPage.this, SubjectView.class);
					intent.putExtra("subject", tv.getText());
					intent.putExtra("student", student.getFirstName());
				startActivity(intent);
				}
				
			};
			
			tv.setOnClickListener(ocl);
			tr.addView(tv);
			tl.addView(tr);
		}
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
	
	public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection

        switch (item.getItemId()) {
        	case R.id.subject:
        		Intent intent = new Intent(OpenPage.this, CreateSubject.class);
        		intent.putExtra("student", student.getFirstName());
        		startActivity(intent);
        		return true;
        	case R.id.save:
        		SaveLoad.save(getApplicationContext());
        		return true;
        	case R.id.load:
        		SaveLoad.load(getApplicationContext());
        		return true;
        	default:
                return super.onOptionsItemSelected(item);
       }
	}
	
}
